package com.hkyp.ss.course.web.controller;

import com.hkyp.ss.course.apis.CourCommVoterService;
import com.hkyp.ss.course.apis.CourseCommentService;
import com.hkyp.ss.course.apis.dto.common.PageReq;
import com.hkyp.ss.course.apis.dto.common.PageResp;
import com.hkyp.ss.course.apis.dto.request.CourseCommentDTO;
import com.hkyp.ss.course.apis.dto.request.UpdateCourseCommentDTO;
import com.hkyp.ss.course.apis.dto.response.CourCommVO;
import com.hkyp.ss.course.apis.dto.response.CourCommVoterVO;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.web.dto.base.ResultBean;
import com.hkyp.ss.course.web.dto.request.VoterDTO;
import com.hkyp.ss.course.web.dto.request.WebUpdateCourseCommentDTO;
import com.hkyp.ss.course.web.dto.response.SimpleCourCommVO;
import com.hkyp.ss.course.web.dto.response.WebCourCommVO;
import com.hkyp.ss.course.web.utils.UserSessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(description = "课评")
@Slf4j
@RestController
@RequestMapping(value = "/a/h/")
public class CourseCommentController {


    @Reference(version = "1.0")
    private CourseCommentService couserCommentService;

    @Reference(version = "1.0")
    CourCommVoterService courCommVoterService;
    /**
     * 课评列表
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课评列表", response = SimpleCourCommVO.class)
    @RequestMapping(value = "/coursecomment/list/{courseId}", method = RequestMethod.GET)
    public ResultBean<PageResp> getCourseCommentList(PageReq pageReq, @PathVariable Long courseId ){

        return new ResultBean<>(couserCommentService.selectCourseCommentList(pageReq,courseId,UserSessionUtils.getUserIdCanBeNull()));

    }

    /**
     * 课评详情
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课评详情", response = WebCourCommVO.class)
    @RequestMapping(value = "/coursecomment/{ccId}", method = RequestMethod.GET)
    public ResultBean<CourCommVO> getCourseComment(PageReq pageReq, @PathVariable Long ccId ){

        return new ResultBean<>(couserCommentService.selectCourseComment(ccId));

    }


    /**
     * 提交课评
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "提交课评")
    @RequestMapping(value = "/api/coursecomment", method = RequestMethod.POST)
    public ResultBean saveCourseComment( @Valid @RequestBody CourseCommentDTO courseCommentDTO){
        courseCommentDTO.setUserId(UserSessionUtils.getUserId());
        couserCommentService.addCourseComment(courseCommentDTO);
        return new ResultBean();

    }

    /**
     * 编辑课评
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "编辑课评")
    @RequestMapping(value = "/api/coursecomment", method = RequestMethod.PUT)
    public ResultBean editCourseComment( @Valid @RequestBody WebUpdateCourseCommentDTO courseCommentDTO){

        UpdateCourseCommentDTO commentDTO = BeanMapper.map(courseCommentDTO, UpdateCourseCommentDTO.class);
        commentDTO.setUserId(UserSessionUtils.getUserId());
        couserCommentService.updateCourseComment(commentDTO);
        return new ResultBean();

    }
    /**
     * 课评点赞/踩
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课评点赞/踩", notes = "点赞/踩 分别传入 type=up/down ，" +
                                       "取消点赞或取消踩导致状态为中立时，传入type=neutral")
    @RequestMapping(value = "/api/coursecomment/{CCId}/voters", method = RequestMethod.POST)
    public ResultBean<CourCommVoterVO> doCourseCommentVoting(@RequestBody
                                              @Valid VoterDTO voterDTO, @PathVariable Long CCId){

        Long userId = UserSessionUtils.getUserId();

        return new ResultBean<>(courCommVoterService.doCourseCommentVoting(voterDTO.getType(),CCId,userId));

    }


}
